package com.uk.ui.windows;

import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.Kontrata;
import com.uk.interfaces.PopupWindowListener;
import com.uk.ui.PopupWindow;
import com.uk.ui.fatura.FaturaForm;
import com.vaadin.data.Validator;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.terminal.UserError;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.PopupView.Content;

public class KontrataSelectWindow extends PopupWindow implements Button.ClickListener{

	private final TextField kontrataId = new TextField("Kodi i kontrates: ");
	private final Button kerko = new Button("Kerko");
	private final Button ktheKontrate = new Button("Kthe kontrate");
	
	private Kontrata selectedKontrata;
	private final IFaturaBean finalFaturaBean;
	
	private final VerticalLayout vl = new VerticalLayout();
	private final VerticalLayout resultLayout = new VerticalLayout();
	
	PopupWindowListener listener;
	
	public KontrataSelectWindow(PopupWindowListener listener , IFaturaBean faturaBean){
		super("Zgjidh kontraten bazuar ne kod", "300px", "300px");
		this.center();
		this.listener = listener;
		this.finalFaturaBean = faturaBean;
		
		
		FormLayout form = new FormLayout();
		form.setWidth("300px");
		form.addComponent(kontrataId);
		form.addComponent(kerko);
		vl.addComponent(form);
		//vl.addComponent(kerko);
		vl.addComponent(this.resultLayout);
		//vl.setComponentAlignment(kerko, Alignment.MIDDLE_RIGHT);
		//vl.setSizeUndefined();
		this.addComponent(vl);
		this.setResizable(false);
		
		this.kontrataId.addValidator(new Validator() {
			
			@Override
			public void validate(Object value) throws InvalidValueException {
				isValid(value);
				
			}
			
			@Override
			public boolean isValid(Object value) {
				if (value == null) return true;
				try {
					Integer id = new Integer(value.toString());
					kontrataId.setComponentError(null);
					return true;
				} catch (Exception e) {
					kontrataId.setComponentError(new UserError("Vlera duhet te jete numer !"));
				}
				return false;
			}
		});
		
		this.kontrataId.setImmediate(true);
		
		this.kerko.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				resultLayout.removeAllComponents();
				try {
					if (kontrataId.getValue() == null || kontrataId.getValue().equals("")) return;
					selectedKontrata = finalFaturaBean.queryKontrata(new Integer(kontrataId.getValue().toString()));
					if (selectedKontrata != null){
						String result = "ID:<strong>"  + selectedKontrata.getId() + "</strong>" +
								"<br/>Emri:<strong>"  + selectedKontrata.getEmri() + "</strong>" +
								"<br/>Mbiemri:<strong>"  + selectedKontrata.getMbiemri() + "</strong>" +
								"<br/>Adresa:<strong>"  + selectedKontrata.getAdresa() + "</strong>" +
								"<br/>Perdorimi:<strong>"  + selectedKontrata.getPerdorimi().getTag() +"</strong>" ;
						
						Label label = new Label(result);
						label.setContentMode(Label.CONTENT_XHTML);
						resultLayout.addComponent(label);	
						resultLayout.addComponent(ktheKontrate);
					}
					else
						resultLayout.addComponent(new Label("Nuk ka kontrate me kodin e dhene."));
					
				} catch (Exception e) {
					resultLayout.addComponent(new Label(e.getMessage()));
				}
			}
		});
	
		this.ktheKontrate.addListener(this);
	
	}

	public Kontrata getSelectedKontrata() {
		return selectedKontrata;
	}

	@Override
	public void buttonClick(ClickEvent event) {
		this.listener.returnAction(this);
		resultLayout.removeAllComponents();
		this.kontrataId.setValue(null);
		this.close();
		
	}
	
	
}
